/**
 * 
 */
package unseen.labs.games.mysnake.model;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

/**
 * @author PERRAUDIN Steven <sperraudin@neotilus.com>
 *
 * @date 4 juin 2010
 */
public class Ball{
	
private int _nrOfVertices = 0;
	
	// a raw buffer to hold indices allowing a reuse of points.
    private ShortBuffer _indexBuffer;
    
    // a raw buffer to hold the vertices
    private FloatBuffer _vertexBuffer;
    
    private static final int NORTH 		=	1;
    private static final int SOUTH		=	2;
    private static final int WEST 		=	3;
    private static final int EAST		=	4;
    
    private static final int NORTHEAST	=	5;
    private static final int NORTHWEST	=	6;
    private static final int SOUTHEAST	=	7;
    private static final int SOUTHWEST	=	8;
    
    private int movement			= 1;
    
    private float x					= 1;
    private float y					= 1;
        
	public Ball(){		
		initView();		
	}
		
	private void initView(){
		
		float[] _coords = {
				0f, 		0f, 		0f, 	// (x1, y1, z1)
		        0f, 		0.03f, 		0f, 	// (x2, y2, z2)
		        0.025f, 	0.02f, 		0f, 	// (x3, y3, z3)
		        
		        0f, 		0f, 		0f, 	// (x4, y4, z4)
		        0.025f, 	0.02f, 		0f, 	// (x5, y5, z5)
		        0.03f, 		0f, 		0f, 	// (x6, y6, z6)
		        
		        0f, 		0f, 		0f, 	// (x7, y7, z7)
		        0.03f, 		0f, 		0f, 	// (x8, y8, z8)
		        0.025f, 	-0.02f, 	0f, 	// (x9, y9, z9)
		        
		        0f, 		0f, 		0f, 	// (x10, y10, z10)
		        0.025f, 	-0.02f, 	0f, 	// (x11, y11, z11)
		        0f, 		-0.03f, 	0f, 	// (x12, y12, z12)
		        
		        0f, 		0f, 		0f, 	// (x13, y13, z13)
		        0f, 		-0.03f, 	0f, 	// (x14, y14, z14)
		        -0.025f, 	-0.02f, 	0f, 	// (x15, y14, z15)
		        
		        0f, 		0f, 		0f, 	// (x16, y16, z16)
		        -0.025f, 	-0.02f, 	0f, 	// (x17, y17, z17)
		        -0.03f, 	0f, 		0f, 	// (x18, y18, z18)
		        
		        0f, 		0f, 		0f, 	// (x19, y19, z19)
		        -0.03f, 	0f, 		0f, 	// (x20, y20, z20)
		        -0.025f, 	0.02f, 		0f, 	// (x21, y21, z21)
		        
		        0f, 		0f, 		0f, 	// (x22, y22, z22)
		        -0.025f, 	0.02f, 		0f, 	// (x23, y23, z23)
		        0f, 		0.03f, 		0f, 	// (x24, y24, z24)
		        
        };
        _nrOfVertices 	= _coords.length;
        
        short[] indices = {	0, 1, 2, 
        					3, 4, 5,
        					6, 7, 8,
        					9, 10, 11,
        					12, 13, 14,
        					15, 16, 17,
        					18, 19, 20,
        					21, 22, 23
        					};
		
     // float has 4 bytes, coordinate * 4 bytes
        ByteBuffer vbb = ByteBuffer.allocateDirect(_coords.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        _vertexBuffer = vbb.asFloatBuffer();
		
     // short has 2 bytes, indices * 2 bytes
        ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices * 2);
        ibb.order(ByteOrder.nativeOrder());
        _indexBuffer = ibb.asShortBuffer();
        
        _vertexBuffer.put(_coords);
        _indexBuffer.put(indices);
        
        _vertexBuffer.position(0);
        _indexBuffer.position(0);
		
	}
		
	public void move(){
		
		switch(movement){
			case NORTH :
				moveUp(x, y);
				break;
			case SOUTH :
				moveDown(x, y);
				break;
			case WEST :
				moveLeft(x, y);
				break;
			case EAST :
				moveRight(x, y);
				break;
			case NORTHEAST :
				moveUpRight(x, y);
				break;
			case NORTHWEST :
				moveUpLeft(x, y);
				break;
			case SOUTHEAST :
				moveDownRight(x, y);
				break;
			case SOUTHWEST :
				moveDownLeft(x, y);
				break;
		}
		
	}
		
	private void moveDownLeft(float x2, float y2) {
		moveDown(x2, y2);
		moveLeft(x2, y2);
	}

	private void moveDownRight(float x2, float y2) {
		moveDown(x2, y2);
		moveRight(x2, y2);
	}

	private void moveUpLeft(float x2, float y2) {
		moveUp(x2, y2);
		moveLeft(x2, y2);
	}

	private void moveUpRight(float x2, float y2) {
		moveUp(x2, y2);
		moveRight(x2, y2);
	}

	public void moveDown(float x, float y){
		
		if(_vertexBuffer.get(43) > -1 ){
	
			_vertexBuffer.put(1, _vertexBuffer.get(1) - (0.05f * x));
			_vertexBuffer.put(4, _vertexBuffer.get(4) - (0.05f * x));
			_vertexBuffer.put(7, _vertexBuffer.get(7) - (0.05f * x));
			
			_vertexBuffer.put(10, _vertexBuffer.get(10) - (0.05f * x));
			_vertexBuffer.put(13, _vertexBuffer.get(13) - (0.05f * x));
			_vertexBuffer.put(16, _vertexBuffer.get(16) - (0.05f * x));
	
			_vertexBuffer.put(19, _vertexBuffer.get(19) - (0.05f * x));
			_vertexBuffer.put(22, _vertexBuffer.get(22) - (0.05f * x));
			_vertexBuffer.put(25, _vertexBuffer.get(25) - (0.05f * x));
			
			_vertexBuffer.put(28, _vertexBuffer.get(28) - (0.05f * x));
			_vertexBuffer.put(31, _vertexBuffer.get(31) - (0.05f * x));
			_vertexBuffer.put(34, _vertexBuffer.get(34) - (0.05f * x));
	
			_vertexBuffer.put(37, _vertexBuffer.get(37) - (0.05f * x));
			_vertexBuffer.put(40, _vertexBuffer.get(40) - (0.05f * x));
			_vertexBuffer.put(43, _vertexBuffer.get(43) - (0.05f * x));
			
			_vertexBuffer.put(46, _vertexBuffer.get(46) - (0.05f * x));
			_vertexBuffer.put(49, _vertexBuffer.get(49) - (0.05f * x));
			_vertexBuffer.put(52, _vertexBuffer.get(52) - (0.05f * x));
	
			_vertexBuffer.put(55, _vertexBuffer.get(55) - (0.05f * x));
			_vertexBuffer.put(58, _vertexBuffer.get(58) - (0.05f * x));
			_vertexBuffer.put(61, _vertexBuffer.get(61) - (0.05f * x));
			
			_vertexBuffer.put(64, _vertexBuffer.get(64) - (0.05f * x));
			_vertexBuffer.put(67, _vertexBuffer.get(67) - (0.05f * x));
			_vertexBuffer.put(70, _vertexBuffer.get(70) - (0.05f * x));
		}
		else{
			if (movement == SOUTHEAST)
				movement = NORTHEAST;
			else if (movement == SOUTHWEST)
				movement = NORTHWEST;
			else
				movement = NORTH;
		}
	}
	
	public void moveUp(float x, float y){
		if(_vertexBuffer.get(1) < 1){
				
			_vertexBuffer.put(1, _vertexBuffer.get(1) + (0.05f * x));
			_vertexBuffer.put(4, _vertexBuffer.get(4) + (0.05f * x));
			_vertexBuffer.put(7, _vertexBuffer.get(7) + (0.05f * x));
			
			_vertexBuffer.put(10, _vertexBuffer.get(10) + (0.05f * x));
			_vertexBuffer.put(13, _vertexBuffer.get(13) + (0.05f * x));
			_vertexBuffer.put(16, _vertexBuffer.get(16) + (0.05f * x));
	
			_vertexBuffer.put(19, _vertexBuffer.get(19) + (0.05f * x));
			_vertexBuffer.put(22, _vertexBuffer.get(22) + (0.05f * x));
			_vertexBuffer.put(25, _vertexBuffer.get(25) + (0.05f * x));
			
			_vertexBuffer.put(28, _vertexBuffer.get(28) + (0.05f * x));
			_vertexBuffer.put(31, _vertexBuffer.get(31) + (0.05f * x));
			_vertexBuffer.put(34, _vertexBuffer.get(34) + (0.05f * x));
	
			_vertexBuffer.put(37, _vertexBuffer.get(37) + (0.05f * x));
			_vertexBuffer.put(40, _vertexBuffer.get(40) + (0.05f * x));
			_vertexBuffer.put(43, _vertexBuffer.get(43) + (0.05f * x));
			
			_vertexBuffer.put(46, _vertexBuffer.get(46) + (0.05f * x));
			_vertexBuffer.put(49, _vertexBuffer.get(49) + (0.05f * x));
			_vertexBuffer.put(52, _vertexBuffer.get(52) + (0.05f * x));
	
			_vertexBuffer.put(55, _vertexBuffer.get(55) + (0.05f * x));
			_vertexBuffer.put(58, _vertexBuffer.get(58) + (0.05f * x));
			_vertexBuffer.put(61, _vertexBuffer.get(61) + (0.05f * x));
			
			_vertexBuffer.put(64, _vertexBuffer.get(64) + (0.05f * x));
			_vertexBuffer.put(67, _vertexBuffer.get(67) + (0.05f * x));
			_vertexBuffer.put(70, _vertexBuffer.get(70) + (0.05f * x));
		}
		else{
			
			if (movement == NORTHEAST)
				movement = SOUTHEAST;
			else if (movement == NORTHWEST)
				movement = SOUTHWEST;
			else
				movement = SOUTH;
		}
		
		
	}
	
	public void moveLeft(float x, float y){

		if(_vertexBuffer.get(57) > -1){
			
			_vertexBuffer.put(0, _vertexBuffer.get(0) - (0.05f * y));
			_vertexBuffer.put(3, _vertexBuffer.get(3) - (0.05f * y));
			_vertexBuffer.put(6, _vertexBuffer.get(6) - (0.05f * y));
			
			_vertexBuffer.put(9, _vertexBuffer.get(9) - (0.05f * y));
			_vertexBuffer.put(12, _vertexBuffer.get(12) - (0.05f * y));
			_vertexBuffer.put(15, _vertexBuffer.get(15) - (0.05f * y));
	
			_vertexBuffer.put(18, _vertexBuffer.get(18) - (0.05f * y));
			_vertexBuffer.put(21, _vertexBuffer.get(21) - (0.05f * y));
			_vertexBuffer.put(24, _vertexBuffer.get(24) - (0.05f * y));
			
			_vertexBuffer.put(27, _vertexBuffer.get(27) - (0.05f * y));
			_vertexBuffer.put(30, _vertexBuffer.get(30) - (0.05f * y));
			_vertexBuffer.put(33, _vertexBuffer.get(33) - (0.05f * y));
	
			_vertexBuffer.put(36, _vertexBuffer.get(36) - (0.05f * y));
			_vertexBuffer.put(39, _vertexBuffer.get(39) - (0.05f * y));
			_vertexBuffer.put(42, _vertexBuffer.get(42) - (0.05f * y));
			
			_vertexBuffer.put(45, _vertexBuffer.get(45) - (0.05f * y));
			_vertexBuffer.put(48, _vertexBuffer.get(48) - (0.05f * y));
			_vertexBuffer.put(51, _vertexBuffer.get(51) - (0.05f * y));
	
			_vertexBuffer.put(54, _vertexBuffer.get(54) - (0.05f * y));
			_vertexBuffer.put(57, _vertexBuffer.get(57) - (0.05f * y));
			_vertexBuffer.put(60, _vertexBuffer.get(60) - (0.05f * y));
			
			_vertexBuffer.put(63, _vertexBuffer.get(63) - (0.05f * y));
			_vertexBuffer.put(66, _vertexBuffer.get(66) - (0.05f * y));
			_vertexBuffer.put(69, _vertexBuffer.get(69) - (0.05f * y));
		}
		else{
			if (movement == NORTHWEST)
				movement = NORTHEAST;
			else if (movement == SOUTHWEST)
				movement = SOUTHEAST;
			else
				movement = EAST;
		}		
		
	}

	public void moveRight(float x, float y){
		if(_vertexBuffer.get(15) < 1){
			
			_vertexBuffer.put(0, _vertexBuffer.get(0) + (0.05f * y));
			_vertexBuffer.put(3, _vertexBuffer.get(3) + (0.05f * y));
			_vertexBuffer.put(6, _vertexBuffer.get(6) + (0.05f * y));
			
			_vertexBuffer.put(9, _vertexBuffer.get(9) + (0.05f * y));
			_vertexBuffer.put(12, _vertexBuffer.get(12) + (0.05f * y));
			_vertexBuffer.put(15, _vertexBuffer.get(15) + (0.05f * y));
	
			_vertexBuffer.put(18, _vertexBuffer.get(18) + (0.05f * y));
			_vertexBuffer.put(21, _vertexBuffer.get(21) + (0.05f * y));
			_vertexBuffer.put(24, _vertexBuffer.get(24) + (0.05f * y));
			
			_vertexBuffer.put(27, _vertexBuffer.get(27) + (0.05f * y));
			_vertexBuffer.put(30, _vertexBuffer.get(30) + (0.05f * y));
			_vertexBuffer.put(33, _vertexBuffer.get(33) + (0.05f * y));
	
			_vertexBuffer.put(36, _vertexBuffer.get(36) + (0.05f * y));
			_vertexBuffer.put(39, _vertexBuffer.get(39) + (0.05f * y));
			_vertexBuffer.put(42, _vertexBuffer.get(42) + (0.05f * y));
			
			_vertexBuffer.put(45, _vertexBuffer.get(45) + (0.05f * y));
			_vertexBuffer.put(48, _vertexBuffer.get(48) + (0.05f * y));
			_vertexBuffer.put(51, _vertexBuffer.get(51) + (0.05f * y));
	
			_vertexBuffer.put(54, _vertexBuffer.get(54) + (0.05f * y));
			_vertexBuffer.put(57, _vertexBuffer.get(57) + (0.05f * y));
			_vertexBuffer.put(60, _vertexBuffer.get(60) + (0.05f * y));
			
			_vertexBuffer.put(63, _vertexBuffer.get(63) + (0.05f * y));
			_vertexBuffer.put(66, _vertexBuffer.get(66) + (0.05f * y));
			_vertexBuffer.put(69, _vertexBuffer.get(69) + (0.05f * y));
		}
		else{
			if (movement == NORTHEAST)
				movement = NORTHWEST;
			else if (movement == SOUTHEAST)
				movement = SOUTHWEST;
			else
				movement = WEST;
		}
	}
	
	/**
	 * @return the _nrOfVertices
	 */
	public int getNrOfVertices() {
		return _nrOfVertices;
	}

	/**
	 * @param nrOfVertices the _nrOfVertices to set
	 */
	public void setNrOfVertices(int nrOfVertices) {
		_nrOfVertices = nrOfVertices;
	}

	/**
	 * @return the _indexBuffer
	 */
	public ShortBuffer getIndexBuffer() {
		return _indexBuffer;
	}


	/**
	 * @param indexBuffer the _indexBuffer to set
	 */
	public void setIndexBuffer(ShortBuffer indexBuffer) {
		_indexBuffer = indexBuffer;
	}

	/**
	 * @return the _vertexBuffer
	 */
	public FloatBuffer getVertexBuffer() {
		return _vertexBuffer;
	}


	/**
	 * @param vertexBuffer the _vertexBuffer to set
	 */
	public void setVertexBuffer(FloatBuffer vertexBuffer) {
		_vertexBuffer = vertexBuffer;
	}

	/**
	 * @param movement the movement to set
	 */
	public void setMovement(int movement) {
		this.movement = movement;
	}

	public boolean tagada(Racket rRacket) {
		
		if(_vertexBuffer.get(34) < rRacket.getVertexBuffer().get(7) && 			// TOP RACKET
				_vertexBuffer.get(34) < rRacket.getVertexBuffer().get(6) 		// MIDDLE RACKET
				&& _vertexBuffer.get(0) >= rRacket.getVertexBuffer().get(0) 	// LEFT CORNER
				&& _vertexBuffer.get(0) <= rRacket.getVertexBuffer().get(3)		// RIGHT CORNER
				)
		{
			this.movement 	= NORTHWEST;
			this.x 			= (float)(Math.random() * 1);
			this.y 			= (float)(Math.random() * 1);
			return true;
		}
		if(_vertexBuffer.get(34) < rRacket.getVertexBuffer().get(7) && 
				_vertexBuffer.get(34) > rRacket.getVertexBuffer().get(6) 
				&& _vertexBuffer.get(0) > rRacket.getVertexBuffer().get(0)  	// LEFT CORNER
				&& _vertexBuffer.get(0) < rRacket.getVertexBuffer().get(3)		// RIGHT CORNER
				)
		{
			this.movement 	= NORTHEAST;
			this.x 			= (float)(Math.random() * 1);
			this.y 			= (float)(Math.random() * 1);
			return true;
		}
		
		if(_vertexBuffer.get(34) < -1){	// GAME OVER 
			return false;
		}
		return true;
		
	}

}
